package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.system.service.ISupplyCategroyService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Supplier;
import com.ruoyi.system.service.ISupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供货商Controller
 * 
 * @author ruoyi
 * @date 2025-07-28
 */
@RestController
@RequestMapping("/supply/supplier")
public class SupplierController extends BaseController
{
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private ISupplyCategroyService supplyCategroyService;

    /**
     * 审核供应商
     * @param id
     * @param status
     * @param reviewRemark
     * @return
     */
    @RequestMapping("/reviewSupply")
    public AjaxResult reviewSupply(@RequestParam("id") long id,@RequestParam("status")String status,@RequestParam("reviewRemark")String reviewRemark){
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplier.setStatus(status);
        supplier.setReviewRemark(reviewRemark);
        supplier.setReviewTime(IOUtil.getdate("yyyy-MM-dd HH:mm:ss"));

        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 将基础商品信息同步给该供货商
     * @param supplierId
     * @return
     */
    @RequestMapping("/syncGoods")
    public AjaxResult syncGoods(@RequestParam("supplierId") long supplierId){
        AjaxResult result = supplyCategroyService.syncGoods(supplierId);
        return result;
    }
    /**
     * 查询供货商列表
     */
    @PreAuthorize("@ss.hasPermi('supply:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier)
    {
        startPage();
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        return getDataTable(list);
    }

    /**
     * 导出供货商列表
     */
    @PreAuthorize("@ss.hasPermi('supply:supplier:export')")
    @Log(title = "供货商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supplier supplier)
    {
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        util.exportExcel(response, list, "供货商数据");
    }

    /**
     * 获取供货商详细信息
     */
    @PreAuthorize("@ss.hasPermi('supply:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(supplierService.selectSupplierById(id));
    }

    /**
     * 新增供货商
     */
    @PreAuthorize("@ss.hasPermi('supply:supplier:add')")
    @Log(title = "供货商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Supplier supplier)
    {
        return supplierService.insertSupplier(supplier);
    }

    /**
     * 修改供货商
     */
    @PreAuthorize("@ss.hasPermi('supply:supplier:edit')")
    @Log(title = "供货商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier)
    {
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除供货商
     */
    @PreAuthorize("@ss.hasPermi('supply:supplier:remove')")
    @Log(title = "供货商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return supplierService.deleteSupplierByIds(ids);
    }
}
